package appcus

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/common/response"
	"counter-help/admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type CusOrderViewApi struct{}

// CreateOrderView 创建订单浏览记录
// @Tags CusOrder
// @Summary 创建订单浏览记录
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param orderID query string true "订单ID"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /cusOrder/createOrderView [post]
func (cusOrderViewApi *CusOrderViewApi) CreateOrderView(c *gin.Context) {
	type ViewRequest struct {
		OrderID int `json:"orderID" binding:"required"`
	}

	var req ViewRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage("参数无效", c)
		return
	}

	userID := utils.GetUserID(c)
	err := cusOrderViewService.CreateOrderView(int(userID), req.OrderID)
	if err != nil {
		global.GVA_LOG.Error("创建浏览记录失败!", zap.Error(err))
		response.FailWithMessage("创建浏览记录失败:"+err.Error(), c)
		return
	}

	response.OkWithMessage("创建成功", c)
}

// GetOrderViews 获取浏览的订单列表
// @Tags CusOrder
// @Summary 获取浏览的订单列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=[]customer.CusOrder,msg=string} "获取成功"
// @Router /cusOrder/getOrderViews [get]
func (cusOrderViewApi *CusOrderViewApi) GetOrderViews(c *gin.Context) {
	userID := utils.GetUserID(c)
	orderViews, err := cusOrderViewService.GetOrderViewsByUserID(int(userID))
	if err != nil {
		global.GVA_LOG.Error("获取浏览记录失败!", zap.Error(err))
		response.FailWithMessage("获取浏览记录失败:"+err.Error(), c)
		return
	}

	response.OkWithDetailed(orderViews, "获取成功", c)
}
